/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-07-07 15:27:12
 * @LastEditors: cuidm
 * @LastEditTime: 2022-09-30 11:22:00
 */
import React, { useState, useEffect, useRef } from "react";
import { Form, Button, Input, Affix, Select, message, Space, Switch, Drawer, Row, Col, InputNumber,Modal } from "antd";
import { MinusCircleOutlined, PlusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { ModelFormItem } from "@/components";
import cmdbres from "@/data/cmdbres";
import request, {requestIAM} from '@/utils/request';
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";
import moment from 'moment';

const user = hcpUtils.getUser();
const CreateBusiness = ({ history, visible, setVisible, onCreateFinish, info, cmModuleId }) => {
    const modelFormItemRef = useRef();
	const [form] = Form.useForm();
    const [loading, setLoading] = useState(false);
   
    useEffect(() => {
		if(info) {
            if(info.time){
                info.time = hcpUtils.timeConversion(info.time, 'YYYY-MM-DD HH:mm:ss')
            }
            form.setFieldsValue({
                ...info
            });
		}
	}, [info]);

	const onFinish = async (fieldsValue) => {
        modelFormItemRef.current.dealDataFn(fieldsValue);
        console.log(fieldsValue);
        setLoading(true);
        let url = cmdbres.createBiz;
        let param = null;
        let tip = "";
        if(info) {//修改
            url = cmdbres.updateBiz(info.cm_unid);
            param = {
                ...fieldsValue,
                author: user.staffName,
            }
            tip = "修改";
        } else {
            param = {
                cmModuleId,
                data: {
                    ...fieldsValue,
                    author: user.staffName,
                    cm_appkey: "控制台",
                    cm_isdeleted: "N",
                    biz_isdeleted: "N",
                }
            };
        }

		let res = await request({
			...url,
			data: param
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success(`保存成功！`);
			if (typeof onCreateFinish == "function") onCreateFinish(res);
		}
	};

    const handleClose = () => {
        setVisible(false);
    }

    return (
        <Modal
			title="新建实例"
			className="hcp-modal-md create-field"
            width={600}
			visible={visible}
			footer={null}
			onCancel={handleClose}
			destroyOnClose
			maskClosable={false}
			afterClose={()=>{form.resetFields()}}
            style={{
                paddingLeft:0
            }}
		>
            <Form
                className="hcp-drawer-form"
                form={form}
                labelCol={{ span: 5}}
                wrapperCol={{ span: 17 }}
                name="basic"
                onFinish={onFinish}
            >
                <ModelFormItem form={form} cRef={modelFormItemRef} cmModuleId={cmModuleId}/>
                <div className="modal-body-footer" style={{paddingTop: "12px",  position: 'absolute', bottom: 10, paddingLeft:'48px', right:0}}>
                    <Space>
                        <Button type="primary" htmlType="submit" loading={loading}>提交</Button>
                        <Button onClick={handleClose}>取消</Button>
                    </Space>
                </div>
            </Form>
        </Modal>
    );
};

export default CreateBusiness;
